package yg.engine;

import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGL11;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.util.AttributeSet;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceView;
import android.view.SurfaceHolder;

public class YoghurtGum extends Activity 
{

	@Override 
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		// fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags (
			WindowManager.LayoutParams.FLAG_FULLSCREEN,
			WindowManager.LayoutParams.FLAG_FULLSCREEN
		); 
		
		m_View = new YGView(this);
		//m_View.setText("Initializing");
		setContentView(m_View);
	}

	@Override 
	protected void onPause() 
	{
		super.onPause();
		//m_View.onPause();
	}

	@Override 
	protected void onResume() 
	{
		super.onResume();
		//m_View.onResume();
	}

	private SurfaceView m_View;
		
}

//class YGView extends SurfaceView implements SurfaceHolder.Callback
class YGView extends GLSurfaceView
{

	public YGView(Context context) 
	{
        super(context);
        m_Renderer = new YGRenderer();
		setRenderer(m_Renderer);
    }
   
	/*public boolean onTouchEvent(final MotionEvent event) 
	{
		if (event.getAction() == MotionEvent.ACTION_DOWN) 
		{
			NativePause();
		}
		return true;
	}*/

	YGRenderer m_Renderer;

	private static native void NativePause();
	
}

class YGRenderer implements GLSurfaceView.Renderer
{

	YGRenderer()
	{
		super();
	}
	
	public void Init(SurfaceHolder a_Holder)
	{
		System.out.println("YGRenderer: Init");
		
		m_EGL = (EGL10)EGLContext.getEGL();
		if (m_EGL == null) 
		{
			throw new RuntimeException("Couldn't initialize OpenGL ES");
		}
		
		m_EGLDisplay = m_EGL.eglGetDisplay(EGL11.EGL_DEFAULT_DISPLAY);
		if (m_EGLDisplay == null) 
		{
			throw new RuntimeException("Couldn't open connection to display");
		}
		
		int[] version = new int[2];
		if (!m_EGL.eglInitialize(m_EGLDisplay, version)) 
		{
			throw new RuntimeException("Couldn't initialize OpenGL ES display");
		}
		
		System.out.println("EGL version: " + version[0] + "." + version[1]);
		
		int[] color_config = new int[] 
		{
			EGL11.EGL_RED_SIZE, 5,
			EGL11.EGL_GREEN_SIZE, 6,
			EGL11.EGL_BLUE_SIZE, 5,
			EGL11.EGL_ALPHA_SIZE, 0,
			EGL11.EGL_DEPTH_SIZE, 16,
			EGL11.EGL_SURFACE_TYPE, EGL11.EGL_WINDOW_BIT,
			EGL11.EGL_NONE
		};
		
		int[] num_config = new int[1];
		m_EGL.eglChooseConfig(m_EGLDisplay, color_config, null, 0, num_config);
		int numConfigs = num_config[0];

		if (numConfigs <= 0) 
		{
			throw new IllegalArgumentException("No configs match configSpec");
		}

		EGLConfig[] configs = new EGLConfig[numConfigs];
		m_EGL.eglChooseConfig(m_EGLDisplay, color_config, configs, numConfigs, num_config);
		m_EGLConfig = configs[0];
		
		if (m_EGLConfig == null)
		{
			throw new RuntimeException("Config failed");
		}
		
		m_EGLContext = m_EGL.eglCreateContext(m_EGLDisplay, m_EGLConfig, EGL11.EGL_NO_CONTEXT, null);
		if (m_EGLContext == null || m_EGLContext == EGL11.EGL_NO_CONTEXT) 
		{
			throw new RuntimeException("CreateContext failed");
		}
		
		m_EGLSurface = null;
		
		m_GL = (GL10)m_EGLContext.getGL();
		
		System.out.println("YGRenderThread: CreateSurface");
		
		if (m_EGLDisplay == null)
		{
			throw new RuntimeException("m_EGLDisplay");
		}
		
		if (m_EGLConfig == null)
		{
			throw new RuntimeException("m_EGLConfig");
		}
		
		if (a_Holder == null)
		{
			throw new RuntimeException("a_Holder");
		}
		
		m_EGLSurface = m_EGL.eglCreateWindowSurface(m_EGLDisplay, m_EGLConfig, a_Holder, null);
		if (m_EGLSurface == null || m_EGLSurface == EGL10.EGL_NO_SURFACE) 
		{
			throwEglException("CreateWindowSurface");
		}
		
		if (!m_EGL.eglMakeCurrent(m_EGLDisplay, m_EGLSurface, m_EGLSurface, m_EGLContext)) 
		{
			throwEglException("eglMakeCurrent");
		}
		
		System.out.println("Created surface");
	}

	 public void onSurfaceChanged(GL10 gl, int w, int h)
	 {
		// do nothing, because we're always full screen
	 }

	public void onSurfaceCreated(GL10 gl, EGLConfig config)
	{	
		System.out.println("YGRenderer: onSurfaceCreated");
		
		NativeInit();
	}
	
	public void surfaceDestroyed() 
	{
	
	}

	public void onDrawFrame(GL10 gl) 
	{
		NativeRender();
    }
	
	private void throwEglException(String function) 
	{
		throw new RuntimeException(function + " failed: " + m_EGL.eglGetError());
	}
	
	/*
		Native functions
	*/

	private native void NativeInit();
	private native void NativeRender();
	private native void NativeDone();
   
	public GL10        m_GL;
	public EGL10       m_EGL;
    public EGLDisplay  m_EGLDisplay;
    public EGLSurface  m_EGLSurface;
    public EGLConfig   m_EGLConfig;
    public EGLContext  m_EGLContext;
}
